package com.cms.web.help;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.biz.help.HelpServiceMgr;
import com.cms.biz.help.CMSDictHelpServiceMgr;
import com.cms.pub.HTXMDataSet2JsonUtil;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.dict.DMTServiceMgr;
import com.efounder.eai.data.JParamObject;

import com.metadata.biz.MetaDataServiceMgr;
import com.metadata.persistence.SYS_DICTS;
import com.web.BaseController;

@Controller
@RequestMapping(value="/CMSDictHelp")
public class HelpController extends BaseController{
	
	@Autowired
	private                            DMTServiceMgr                DMTServiceMgr;
	
	@Autowired
	private                           HelpServiceMgr               helpServiceMgr;
	@Autowired   
	private                    CMSDictHelpServiceMgr           dictHelpServiceMgr;
	@Autowired
	private                       MetaDataServiceMgr           metaDataServiceMgr;
	
	@Autowired
	public                               HttpSession                      session;

	/**
	 * 客户属性列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/dictHelp")
	public String dictHelp(@RequestParam("dictColumn") String dictColumn, @RequestParam("tableName") String tableName, 
			               @RequestParam("returnUrl") String returnUrl, @RequestParam("DCT_BMCOL") String DCT_BMCOL,
			               @RequestParam("DCT_MCCOL") String DCT_MCCOL, Model model) {	
		EFDataSet dataset = helpServiceMgr.helpDict(tableName, dictColumn, " F_SYZT = '1'");
		model.addAttribute("dictColumn", dictColumn);
		model.addAttribute("tableName", tableName);
		model.addAttribute("returnUrl", returnUrl);
		model.addAttribute("DCT_BMCOL", DCT_BMCOL);
		model.addAttribute("DCT_MCCOL", DCT_MCCOL);
		model.addAttribute("dataset", dataset);
		return returnUrl;
	}
	
	@RequestMapping("/contractHelp")
	public String contractHelp(@RequestParam("dictColumn") String dictColumn, @RequestParam("tableName") String tableName, 
			                   @RequestParam("returnUrl") String returnUrl, @RequestParam("DCT_BMCOL") String DCT_BMCOL,
			                   @RequestParam("DCT_MCCOL") String DCT_MCCOL, @RequestParam("F_BMBH") String F_BMBH, 
			                   @RequestParam("F_DJLX") String F_DJLX, Model model) {	
		EFDataSet dataset = helpServiceMgr.helpDict(tableName, dictColumn, " F_BMBH = '" + F_BMBH + "' and F_DJLX = '" + F_DJLX + "' and F_SYZT = '1'");
		model.addAttribute("dictColumn", dictColumn);
		model.addAttribute("tableName", tableName);
		model.addAttribute("returnUrl", returnUrl);
		model.addAttribute("DCT_BMCOL", DCT_BMCOL);
		model.addAttribute("DCT_MCCOL", DCT_MCCOL);
		model.addAttribute("dataset", dataset);
		return returnUrl;
	}
	
	@RequestMapping("/FCTHelp")
	public String dictHelp(@RequestParam("dictColumn") String dictColumn, @RequestParam("tableName") String tableName, 
			               @RequestParam("returnUrl") String returnUrl, @RequestParam("DCT_BMCOL") String DCT_BMCOL,
			               @RequestParam("DCT_MCCOL") String DCT_MCCOL, @RequestParam("BILL_WHERE") String BILL_WHERE, Model model) {	
		EFDataSet dataset = helpServiceMgr.helpDict(tableName, dictColumn, BILL_WHERE);
		model.addAttribute("dictColumn", dictColumn);
		model.addAttribute("tableName", tableName);
		model.addAttribute("returnUrl", returnUrl);
		model.addAttribute("DCT_BMCOL", DCT_BMCOL);
		model.addAttribute("DCT_MCCOL", DCT_MCCOL);
		model.addAttribute("dataset", dataset);
		model.addAttribute("BILL_WHERE", BILL_WHERE);
		return returnUrl;
	}
	
	/**
	 * 客户属性列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/dictHelp/list")
	public String dictHelp(@RequestParam("dictColumn") String dictColumn, @RequestParam("tableName") String tableName, 
			               @RequestParam("returnUrl") String returnUrl, @RequestParam String keywords, 
			               @RequestParam("keywordsColumn") String keywordsColumn, @RequestParam("DCT_BMCOL") String DCT_BMCOL, 
			               @RequestParam("DCT_MCCOL") String DCT_MCCOL, Model model) {
		EFDataSet dataset = helpServiceMgr.helpDict(tableName, dictColumn, " F_SYZT = '1' and " + keywordsColumn + " like '" + keywords + "%'");
		model.addAttribute("dictColumn", dictColumn);
		model.addAttribute("tableName", tableName);
		model.addAttribute("returnUrl", returnUrl);
		model.addAttribute("keywordsColumn", keywordsColumn);
		model.addAttribute("dataset", dataset);
		model.addAttribute("DCT_BMCOL", DCT_BMCOL);
		model.addAttribute("DCT_MCCOL", DCT_MCCOL);
		model.addAttribute("keywords", keywords);
		return returnUrl;
	}
	
	/**
	 * 客户属性列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/FCTHelp/list")
	public String dictHelp(@RequestParam("dictColumn") String dictColumn, @RequestParam("tableName") String tableName, 
			               @RequestParam("returnUrl") String returnUrl, @RequestParam String keywords, 
			               @RequestParam("keywordsColumn") String keywordsColumn, @RequestParam("DCT_BMCOL") String DCT_BMCOL, 
			               @RequestParam("DCT_MCCOL") String DCT_MCCOL, @RequestParam("BILL_WHERE") String BILL_WHERE, Model model) {
		EFDataSet dataset = helpServiceMgr.helpDict(tableName, dictColumn, BILL_WHERE);
		model.addAttribute("dictColumn", dictColumn);
		model.addAttribute("tableName", tableName);
		model.addAttribute("returnUrl", returnUrl);
		model.addAttribute("keywordsColumn", keywordsColumn);
		model.addAttribute("dataset", dataset);
		model.addAttribute("DCT_BMCOL", DCT_BMCOL);
		model.addAttribute("DCT_MCCOL", DCT_MCCOL);
		model.addAttribute("keywords", keywords);
		model.addAttribute("BILL_WHERE", BILL_WHERE);
		return returnUrl;
	}
	
	@RequestMapping("/HTXMHelp")
	public String HTXMHelp(@RequestParam("keywords") String keywords, @RequestParam("F_CODE") String F_CODE, 
			               @RequestParam("DJLX_BH") String DJLX_BH, Model model) {	
		String BILL_WHERE = "";
		EFDataSet dataset = null;
		BILL_WHERE = " HTXXZD.F_XMMC like '" + keywords + "%' and HTXXZD.F_BMBH like '" + F_CODE + "%' and HTXXZD.F_DJLX like '" + DJLX_BH + "%' and HTXXZD.F_SYZT = '1'";
		dataset = dictHelpServiceMgr.getHTXMDataSetRow(keywords, BILL_WHERE);
		model.addAttribute("keywords", keywords);
		model.addAttribute("dataset", dataset);
		model.addAttribute("F_CODE", F_CODE);
		model.addAttribute("DJLX_BH", DJLX_BH);
		return "/cms/help/ybhtxmHelp";
	}
	
	@RequestMapping("/SCTZHTXMHelp")
	public String SCTZHTXMHelp(@RequestParam("keywords") String keywords, Model model) {	
		EFDataSet dataset = dictHelpServiceMgr.getSCTZHTXMDataSetRow(keywords);
		model.addAttribute("keywords", keywords);
		model.addAttribute("dataset", dataset);
		return "/cms/help/ybhtxmHelp1";
	}
	
	@RequestMapping("/WXTZHTXMHelp")
	public String WXTZHTXMHelp(@RequestParam("keywords") String keywords, Model model) {	
		EFDataSet dataset = dictHelpServiceMgr.getWXTZHTXMDataSetRow(keywords);
		model.addAttribute("keywords", keywords);
		model.addAttribute("dataset", dataset);
		return "/cms/help/ybhtxmHelp2";
	}
	
	/**
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/dictHelp/tree")
	public String dictTreeHelp(@RequestParam("dictColumn") String dictColumn, @RequestParam("tableName") String tableName, 
			                   @RequestParam("returnUrl") String returnUrl, @RequestParam String sqlOrder, 
			                   @RequestParam("DCT_BMCOL") String DCT_BMCOL, 
			                   @RequestParam("DCT_MCCOL") String DCT_MCCOL, Model model) {
		
		JParamObject            PO = JParamObject.Create();
		JSONObject      jsonObject = new JSONObject();
		JSONObject     jsonObject1 = new JSONObject();
		JSONArray          jsonArr = new JSONArray();//json格式的数组
		JSONArray         jsonArr1 = new JSONArray();//json格式的数组
		EFDataSet          dataset = EFDataSet.getInstance();
		EFRowSet        dictRowSet = EFRowSet.getInstance();
		String          BILL_WHERE = "";
		
		PO.SetValueByParamName("tableName", tableName);
		PO.SetValueByParamName("sqlColumn", dictColumn);
		PO.SetValueByParamName("dictColumn", dictColumn);
		PO.SetValueByParamName("sqlWhere", " F_SYZT = '1'");
		PO.SetValueByParamName("sqlOrder", sqlOrder);
		
		dataset = helpServiceMgr.helpDict(PO);
		dictRowSet = metaDataServiceMgr.getDictRow(tableName);
		jsonObject = HTXMDataSet2JsonUtil.convertDataSet2Json(dataset, dictRowSet);
		jsonArr = jsonObject.getJSONArray("data");
		jsonObject1.put("text", "合同项目信息");
		jsonObject1.put(dictRowSet.getString(SYS_DICTS._DCT_BMCOLID_, ""), "");
		jsonObject1.put("icon", "a");
		jsonObject1.put("children", jsonArr);
		jsonArr1.add(jsonObject1);
		String json = jsonArr1.toString();
		json = json.replace("\"icon\":\"a\"", "icon:a").replace("\"icon\":\"b\"", "icon:b").replace("\"icon\":\"c\"", "icon:c").replace("\"icon\":\"d\"", "icon:d");
		json = json.replace("\"icon\":\"e\"", "icon:e").replace("\"icon\":\"f\"", "icon:f").replace("\"icon\":\"g\"", "icon:g");
		System.out.println(json);
		
		BILL_WHERE = " F_SYZT = '1' and F_JS = 1";
		
		PO.SetValueByParamName("tableName", tableName);
		PO.SetValueByParamName("sqlColumn", dictColumn);
		PO.SetValueByParamName("dictColumn", dictColumn);
		PO.SetValueByParamName("sqlWhere", BILL_WHERE);
		PO.SetValueByParamName("sqlOrder", sqlOrder);
		
		dataset = helpServiceMgr.helpDict(PO);

		model.addAttribute("listDataset", dataset);
		
		model.addAttribute("treeJson", json);
		model.addAttribute("dictColumn", dictColumn);
		model.addAttribute("tableName", tableName);
		model.addAttribute("sqlOrder", sqlOrder);
		model.addAttribute("returnUrl", returnUrl);
		model.addAttribute("dataset", dataset);
		model.addAttribute("DCT_BMCOL", DCT_BMCOL);
		model.addAttribute("DCT_MCCOL", DCT_MCCOL);
		return returnUrl;
	}
	
	
	/**
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("/dictHelp/treeNextNodeList")
	public String dictTreeNextNodeListHelp(@RequestParam("dictColumn") String dictColumn, @RequestParam("tableName") String tableName, 
			                               @RequestParam("returnUrl") String returnUrl, @RequestParam String sqlOrder, 
			                               @RequestParam("DCT_BMCOL") String DCT_BMCOL, @RequestParam String F_ZDBH, 
			                               @RequestParam("DCT_MCCOL") String DCT_MCCOL, Model model) {
		
		JParamObject            PO = JParamObject.Create();

		EFDataSet          dataset = EFDataSet.getInstance();
		EFRowSet        dictRowSet = EFRowSet.getInstance();
		int                   F_JS = 0;
		String          BILL_WHERE = "";
		
		dictRowSet = DMTServiceMgr.getRow("HTXMZD", "F_XMBH", F_ZDBH);
		
		F_JS = dictRowSet.getNumber("F_JS", 0).intValue();
		
		BILL_WHERE = " F_SYZT = '1' and F_XMBH like '" + F_ZDBH + "%' and F_JS = " + (F_JS + 1);
		
		PO.SetValueByParamName("tableName", tableName);
		PO.SetValueByParamName("sqlColumn", dictColumn);
		PO.SetValueByParamName("dictColumn", dictColumn);
		PO.SetValueByParamName("sqlWhere", BILL_WHERE);
		PO.SetValueByParamName("sqlOrder", sqlOrder);
		
		dataset = helpServiceMgr.helpDict(PO);

		model.addAttribute("listDataset", dataset);
		model.addAttribute("dictColumn", dictColumn);
		model.addAttribute("tableName", tableName);
		model.addAttribute("sqlOrder", sqlOrder);
		model.addAttribute("returnUrl", returnUrl);
		model.addAttribute("DCT_BMCOL", DCT_BMCOL);
		model.addAttribute("DCT_MCCOL", DCT_MCCOL);
		return returnUrl;
	}
}
